package com.xoa.service.vehicle;

import com.xoa.dao.common.SysCodeMapper;
import com.xoa.dao.department.DepartmentMapper;
import com.xoa.dao.vehicle.VehicleOilCardMapper;
import com.xoa.model.common.SysCode;
import com.xoa.model.vehicle.VehicleOilCard;
import com.xoa.service.users.UsersService;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 创建作者:   高亚峰
 * 创建日期:   2017/9/21 10:05
 * 类介绍  :  车辆油卡管理
 * 构造参数:
 */
@Service
public class VehicleOilCardService {

    @Autowired
    VehicleOilCardMapper vehicleOilCardMapper;
    @Autowired
    UsersService usersService;
    @Autowired
    DepartmentMapper departmentMapper;
    @Autowired
    SysCodeMapper sysCodeMapper;
    /**
     * 创建作者:   高亚峰
     * 创建日期:   2017年9月21日 上午10:01:41
     * 方法介绍:   添加油卡
     * 参数说明:   vehicleOilCard
     * @return    ToJson
     */
    public ToJson<Object> addVehicleOilCard(VehicleOilCard vehicleOilCard){
        ToJson<Object> json =new ToJson<Object>(1,"err");
        try {
            int i = vehicleOilCardMapper.insertSelective(vehicleOilCard);
            if(i>0){
                json.setMsg("ok");
                json.setFlag(0);
            }
        } catch (Exception e) {
            json.setFlag(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }
    /**
     * 创建作者:   高亚峰
     * 创建日期:   2017年9月21日 上午10:01:41
     * 方法介绍:   编辑油卡信息
     * 参数说明:   vehicleOilCard
     * @return    ToJson
     */
    public ToJson<Object> editVehicleOilCard(VehicleOilCard vehicleOilCard){
        ToJson<Object> json =new ToJson<Object>(1,"err");
        try {
            int i = vehicleOilCardMapper.updateByPrimaryKeySelective(vehicleOilCard);
            if(i>0){
                json.setMsg("ok");
                json.setFlag(0);
            }
        } catch (Exception e) {
            json.setFlag(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }
    /**
     * 创建作者:   高亚峰
     * 创建日期:   2017年9月21日 上午10:01:41
     * 方法介绍:   删除油卡信息
     * 参数说明:   id
     * @return    ToJson
     */
    public ToJson<Object> deleteOilCard(Integer id){
        ToJson<Object> json =new ToJson<Object>(1,"err");
        try {
             int i=vehicleOilCardMapper.deleteByPrimaryKey(id);
            if(i>0){
                json.setMsg("ok");
                json.setFlag(0);
            }
        } catch (Exception e) {
            json.setFlag(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }
    /**
     * 创建作者:   高亚峰
     * 创建日期:   2017年9月21日 上午10:01:41
     * 方法介绍:   批量删除油卡信息
     * 参数说明:   ids
     * @return    ToJson
     */
    public ToJson<Object> deleteCards(String[]ids){
        ToJson<Object> json =new ToJson<Object>(1,"err");
        try {
           int i=vehicleOilCardMapper.deleteCards(ids);
            if(i>0){
                json.setMsg("ok");
                json.setFlag(0);
            }
        } catch (Exception e) {
            json.setFlag(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }
    /**
     * 创建作者:   高亚峰
     * 创建日期:   2017年9月22日 上午10:01:41
     * 方法介绍:   油卡列表展示
     * 参数说明:   request
     * @return    ToJson
     */
    public ToJson<VehicleOilCard> selectAllCard(HttpServletRequest request){
        ToJson<VehicleOilCard> json =new ToJson<VehicleOilCard>(1,"err");
        try {
            List<VehicleOilCard> vehicleOilCards = vehicleOilCardMapper.selectAllCard();
            for(VehicleOilCard vehicleOilCard:vehicleOilCards){
                if(!StringUtils.checkNull(vehicleOilCard.getvDept())){
                    String depart =vehicleOilCard.getvDept();
                    if (depart.endsWith(",")) {
                        depart=depart.substring(0,depart.length()-1);
                    }
                    vehicleOilCard.setvDeptName(departmentMapper.getDeptNameByDeptId(Integer.valueOf(depart)));
                }

               if(!StringUtils.checkNull(vehicleOilCard.getvOnwer())){
                   String owner =vehicleOilCard.getvOnwer();
                   vehicleOilCard.setvOnwerName(usersService.getUserNamesByUserIds(owner));
               }
               if(!StringUtils.checkNull(vehicleOilCard.getvUser())){
                   String owner =vehicleOilCard.getvUser();
                   vehicleOilCard.setvUserName(usersService.getUserNamesByUserIds(owner));
               }

                //设置车辆类型
                SysCode sysCode = sysCodeMapper.getSingleCode("VEHICLETYPE", vehicleOilCard.getvType());
                if(sysCode!=null){
                    vehicleOilCard.setvTypeName(sysCode.getCodeName());
                }

                //设置车辆状态
                SysCode sysCode1 = sysCodeMapper.getSingleCode("OILCARDSTATUS", vehicleOilCard.getOcStatus());
                if(sysCode1!=null){
                    vehicleOilCard.setOcStatusName(sysCode1.getCodeName());
                }
            }
              json.setObj(vehicleOilCards);
              json.setFlag(0);
              json.setMsg("ok");
        } catch (Exception e) {
            json.setFlag(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }
    /**
     * 创建作者:   高亚峰
     * 创建日期:   2017年9月22日 上午10:01:41
     * 方法介绍:   油卡查询详情
     * 参数说明:   id
     * @return    ToJson
     */
    public ToJson<VehicleOilCard> selectOilCardById(Integer id){
        ToJson<VehicleOilCard> json =new ToJson<VehicleOilCard>(1,"err");
        try {
            VehicleOilCard vehicleOilCard = vehicleOilCardMapper.selectByPrimaryKey(id);
            if(!StringUtils.checkNull(vehicleOilCard.getvDept())){
                String depart =vehicleOilCard.getvDept();;
                vehicleOilCard.setvDeptName(departmentMapper.getDeptNameByDeptId(Integer.valueOf(depart)));
            }

            if(vehicleOilCard.getvOnwer()!=null){
                String owner =vehicleOilCard.getvOnwer();
                vehicleOilCard.setvOnwerName(usersService.getUserNamesByUserIds(owner));
            }
            if(vehicleOilCard.getvUser()!=null){
                String owner =vehicleOilCard.getvUser();
                vehicleOilCard.setvUserName(usersService.getUserNamesByUserIds(owner));
            }

            //设置车辆类型
            SysCode sysCode = sysCodeMapper.getSingleCode("VEHICLETYPE", vehicleOilCard.getvType());
            if(sysCode!=null){
                vehicleOilCard.setvTypeName(sysCode.getCodeName());
            }

            //设置车辆状态
            SysCode sysCode1 = sysCodeMapper.getSingleCode("OILCARDSTATUS", vehicleOilCard.getOcStatus());
            if(sysCode1!=null){
                vehicleOilCard.setOcStatusName(sysCode1.getCodeName());
            }
            json.setObject(vehicleOilCard);
            json.setFlag(0);
            json.setMsg("ok");
        } catch (Exception e) {
            json.setFlag(1);
            json.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return json;
    }

}
